package com.amkai.aes.webservice.binary.objecthandler;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.acsupport.ACLoginSupportSelectorJob;
import com.amkai.aes.aescommon.service.result.acsupport.ACLoginSelectorSelectorResult;
import com.amkai.aes.generatedbeans.organization.BusinessGroup;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.generatedbeans.physstaff.PhysStaff;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class AcLoginSupportSelectorHandler implements JobObjectHandler<ACLoginSupportSelectorJob, Serializable, ACLoginSelectorSelectorResult> {

	@SuppressWarnings("unchecked")
	public ACLoginSelectorSelectorResult process(ACLoginSupportSelectorJob job, Serializable object) throws Exception {
		ACLoginSelectorSelectorResult result = new ACLoginSelectorSelectorResult();
		SessionManager sm = SiteManager.Start();
		try {
			sm.openSession();
			List<PhysStaff> list0 = getPhysStaffs(sm, job);
//			List<Department> list1 = sm.execute("FROM Department ").list();
			//#12575
			BusinessGroup businessGroup = (BusinessGroup) job.getDepartment().getParent();
			List<Department> list1 = sm.execute("SELECT d FROM Department d LEFT JOIN FETCH d.parent WHERE d.parent.id = :bgId").setLong("bgId", businessGroup.getId()).list();
			for (PhysStaff p : list0) {
				p.getName();
				if (p.getSpecialty() != null) {
					p.getSpecialty().getName();
				}
			}
			for (Department d : list1) {
				d.getName();
				//#12575
				BusinessGroup bg = (BusinessGroup)d.getParent();
				bg.getName();
				bg.getKind();
				bg.getLevel();
			}
			result.setDepartments(list1);
			result.setPhysStaffs(list0);
		} catch (Exception e) {
			throw e;
		}
		finally {
			sm.closeSession();
		}
		
		return result;
	}

	// KKORNEL: AES-4438
	@SuppressWarnings("unchecked")
	private List<PhysStaff> getPhysStaffs(SessionManager sm, ACLoginSupportSelectorJob job) {
		Query query = null;
		// KKORNEL: AES-11533
		String passportQ = "SELECT p FROM PhysStaff p " +
			" left join fetch p.person pers "+
			" left join fetch pers.passport pass "+
			"where p.specialty is not null and pass.id = ? and p.baseOrganization.id = ?";
//			//#12575
//			"where p.specialty is not null and pass.id = ? and p.baseOrganization.parent.id = ?";
		
		
		String personQ = "SELECT p FROM PhysStaff p " +
						 " left join fetch p.person pers "+
		                 "WHERE p.specialty IS NOT NULL AND pers.id = ? and p.baseOrganization.id = ?";
		//#12575
		BusinessGroup businessGroup = (BusinessGroup) job.getDepartment().getParent();
		
		if (job.getPassportID() != null) {
			query = sm.execute(passportQ).setLong(0, job.getPassportID()).setLong(1, job.getDepartmentID());
//			query = sm.execute(passportQ).setLong(0, job.getPassportID()).setLong(1, businessGroup.getId());
		}
		else if (job.getPersonID() != null) {
			query = sm.execute(personQ).setLong(0, job.getPersonID()).setLong(1, job.getDepartmentID());
			//#12575
//			query = sm.execute(personQ).setLong(0, job.getPersonID()).setLong(1, businessGroup.getId());			
		}
		return query.list();
	}
}
